//
//  JSProgressHUD.m
//  JSBasicFramework
//
//  Created by Apple on 2018/9/3.
//  Copyright © 2018年 Apple. All rights reserved.
//

#import "JSProgressHUD.h"
#import "JSGraintCircleLayer.h"
#import <MBProgressHUD.h>
#import "MBProgressHUD+NHAdd.h"


#define JSProgressHUDCircle 46
#define JSProgressHUDLeftMargin 15
#define JSProgressHUDMiddleMargin 10
#define JSProgressHUDTopMargin 10

@interface JSProgressHUD ()

@property(nonatomic, weak) JSGraintCircleLayer *circleLayer;
@property(nonatomic, weak) UILabel *messageLabel;
@property(nonatomic, weak) UIView *contentView;

@end

@implementation JSProgressHUD

- (instancetype)initWithFrame:(CGRect)frame

{
    if (self = [super initWithFrame:frame]) {
        [self setupViews];
    }
    return self;
}
- (void)setupViews {
    
    self.backgroundColor=[UIColor colorWithRed:200/255.0 green:200/255.0 blue:200/255.0 alpha:0.2];
    UIView *contentView = [[UIView alloc] init];
    contentView.backgroundColor = [UIColor whiteColor];
    contentView.layer.cornerRadius = 5;
    contentView.clipsToBounds = YES;
    [self addSubview:contentView];
    self.contentView = contentView;
    
    UILabel *label = [[UILabel alloc] init];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor colorWithWhite:0.200 alpha:1.000];
    label.font = [UIFont systemFontOfSize:13];
    label.numberOfLines=0;
    [contentView addSubview:label];
    self.messageLabel = label;
    
    JSGraintCircleLayer *layer = [[JSGraintCircleLayer alloc]
                                  initGraintCircleWithBounds:CGRectMake(0, 0, JSProgressHUDCircle,
                                                                        JSProgressHUDCircle)
                                  Position:CGPointMake(0, 0)
                                  FromColor:[UIColor colorWithWhite:1.000 alpha:0.318]
                                  ToColor:[UIColor colorWithRed:0.122
                                                          green:0.702
                                                           blue:0.820
                                                          alpha:1.000]
                                  LineWidth:3.0];
    [contentView.layer addSublayer:layer];
    self.circleLayer = layer;
}

+ (instancetype)show {
    return [self showWithMessage:nil];
}

+ (instancetype)showWithMessage:(NSString *)message {
    UIView *view = [UIApplication sharedApplication].keyWindow;
    
    return [self createViewToView:view message:message];
}
+ (instancetype)createViewToView:(UIView *)view message:(NSString *)message {
    JSProgressHUD *contentView = [[self alloc] init];
    [view addSubview:contentView];
    return [contentView setViewsMessageAndFrame:message toView:view];
}
- (instancetype)setViewsMessageAndFrame:(NSString *)message
                                 toView:(UIView *)view {
    
    self.frame = view.bounds;
    CGFloat contentWidth;
    CGFloat labelWidth = 56;
    CGFloat labelHeight=0;
    CGPoint circlePoint;
    self.messageLabel.text = message;
    
    if (message.length > 0) {
        labelHeight =
        [message boundingRectWithSize:CGSizeMake(labelWidth, CGFLOAT_MAX)
                              options:NSStringDrawingUsesLineFragmentOrigin
                           attributes:@{
                                        NSFontAttributeName : self.messageLabel.font
                                        }
                              context:nil]
        .size.height;
        contentWidth = MAX(JSProgressHUDCircle + JSProgressHUDLeftMargin * 2,
                           JSProgressHUDCircle + JSProgressHUDTopMargin * 2+JSProgressHUDMiddleMargin+labelHeight);
        circlePoint = CGPointMake(contentWidth / 2,
                                  JSProgressHUDCircle / 2 + JSProgressHUDTopMargin);
    } else {
        contentWidth = MAX(JSProgressHUDCircle + JSProgressHUDLeftMargin * 2,
                           JSProgressHUDCircle + JSProgressHUDTopMargin * 2);
        circlePoint = CGPointMake(contentWidth / 2,contentWidth / 2);
    }
    self.contentView.frame = CGRectMake(0, 0, contentWidth, contentWidth);
    
    self.circleLayer.position = circlePoint;
    self.messageLabel.frame = CGRectMake((contentWidth - labelWidth) / 2,
                                         JSProgressHUDTopMargin + JSProgressHUDCircle +
                                         JSProgressHUDMiddleMargin,
                                         labelWidth, labelHeight);
//    self.contentView.center = view.center;
    self.contentView.center = CGPointMake(view.center.x, view.center.y - (view.frame.origin.y / 2));
    
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI /6 ];
    rotationAnimation.duration = 0.1;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = CGFLOAT_MAX;
    
    [self.circleLayer addAnimation:rotationAnimation forKey:@"ss"];
    
    //    self.messageLabel.backgroundColor=[UIColor colorWithRed:0.414 green:0.667 blue:0.667 alpha:1.000];
    
    return self;
}
+(instancetype)showToView:(UIView *)view message:(NSString *)message
{
    return  [self createViewToView:view message:message];
}
+(void)hideForView:(UIView *)view
{
    [self hideFromView:view];
}
- (void)updateProgress {
    self.circleLayer.transform =
    CATransform3DRotate(self.circleLayer.transform, M_PI / 6, 0, 0, 1);
}
+ (void)hide {
    UIView *view = [UIApplication sharedApplication].keyWindow;
    [self hideFromView:view];
}
+ (void)hideFromView:(UIView *)view {
    NSEnumerator *subviewsEnum = [view.subviews reverseObjectEnumerator];
    for (UIView *subview in subviewsEnum) {
        if ([subview isKindOfClass:self]) {
            [subview removeFromSuperview];
            break;
        }
    }
}

// 显示加载框
+(void)agency_showLoadingInView:(UIView *)view{
    [self agency_showLoadingInView:view noticeMsg:@"加载中..."];
}

+(void)agency_showLoadingInView:(UIView *)view noticeMsg:(NSString *)msg{
    
    if (!view) {
        view = [UIApplication sharedApplication].keyWindow;
    }
    [self createViewToView:view message:msg];
}

+(void)agency_hideForView:(UIView *)view{
    if (view) {
        [self hideFromView:view];
    }else{
        [JSProgressHUD hide];
    }
    
}

+(void)agency_showNoticeMsg:(NSString *)msg InView:(UIView *)view{
    
    if (!view) {
        view = [UIApplication sharedApplication].keyWindow;
    }
    
    [MBProgressHUD showTitleToView:view postion:NHHUDPostionBottom title:msg];

}
+(void)agency_showSuccessMsg:(NSString *)msg InView:(UIView *)view{
    if (!view) {
        view = [UIApplication sharedApplication].keyWindow;
    }
    [MBProgressHUD showTitleToView:view postion:NHHUDPostionBottom title:msg];
}
+(void)agency_showFialdMsg:(NSString *)msg InView:(UIView *)view{
    if (!view) {
        view = [UIApplication sharedApplication].keyWindow;
    }
    [MBProgressHUD showTitleToView:view postion:NHHUDPostionBottom title:msg];
}

+(void)agency_showProgressMsg:(NSString *)msg Inview:(UIView *)view progress:(CGFloat)progress{
    
//    if (!view) {
//        view = [UIApplication sharedApplication].keyWindow;
//    }
//    [MBProgressHUD showDownToView:view progressStyle:NHHUDProgressDeterminateHorizontalBar title:msg progress:^(MBProgressHUD *hud) {
//        hud.allContentColors([UIColor yellowColor]);
//        hud.progressColor([UIColor whiteColor]);
//        dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
//            dispatch_async(dispatch_get_main_queue(), ^{
//                [hud hideAnimated:YES];
//            });
//        });
//    }];
}


@end
